﻿using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using DevExpress.Utils;
using XCI.Core;
using XCI.Extensions;
using XCI.Helper;

namespace XCI.Windows
{
    public partial class ComponentParamGridControl : BaseUserControl
    {
        private List<KeyValueCaption> paramList;

        public ComponentParamGridControl()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 表格控件
        /// </summary>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public GridControl GridControl => grid;

        /// <summary>
        /// 视图控件
        /// </summary>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public GridView GridView => gridView;

        /// <summary>
        /// 数据源(Json字符串)
        /// </summary>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string DataSource
        {
            get => paramList.IsListNotEmpty() ? JsonHelper.ToJsonString(paramList) : string.Empty;
            set
            {
                paramList = WinFormHelper.ResolvingFormParam(value);
                grid.DataSource = new BindingSource { DataSource = paramList };
            }
        }

        /// <summary>
        /// 是否启用编辑
        /// </summary>
        [Browsable(true), Category("XCI"), DefaultValue(true), Description("是否启用编辑")]
        public bool EnableEdit { get; set; } = true;

        /// <summary>
        /// 是否启用删除
        /// </summary>
        [Browsable(true), Category("XCI"), DefaultValue(true), Description("是否启用删除")]
        public bool EnableDelete { get; set; } = true;

        /// <summary>
        /// 数据源
        /// </summary>
        [Browsable(false),Bindable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public object EditValue
        {
            get => DataSource;
            set => DataSource = value.ToStringOrEmpty();
        }

        private void gridViewParam_KeyDown(object sender, KeyEventArgs e)
        {
            if (!EnableDelete) return;
            if (e.KeyCode == Keys.Delete && e.Modifiers == Keys.Control)
            {
                if (MessageBoxHelper.ShowMessageYesNo("确定要删除选中行吗?") != DialogResult.Yes) return;
                gridView.DeleteSelectedRows();
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            gridView.OptionsBehavior.Editable = EnableEdit;
            gridView.OptionsBehavior.AllowAddRows = EnableEdit ? DefaultBoolean.True : DefaultBoolean.False;
            gridView.OptionsBehavior.AllowDeleteRows = EnableEdit ? DefaultBoolean.True : DefaultBoolean.False;
            gridView.OptionsView.NewItemRowPosition= EnableEdit ? NewItemRowPosition.Bottom: NewItemRowPosition.None;

            if (!EnableEdit)
            {
                gridView.OptionsSelection.EnableAppearanceFocusedCell = false;
                gridView.FocusRectStyle = DrawFocusRectStyle.None;
            }
        }
    }
}
